package com.deimos.tests;

import static org.junit.Assert.assertTrue;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.impl.llom.util.AXIOMUtil;
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Test;
import org.vast.ows.sps.GetStatusResponse;
import org.vast.ows.sps.SPSUtils;
import org.vast.ows.sps.StatusReport;
import org.vast.ows.sps.StatusReport.RequestStatus;
import org.vast.xml.DOMHelper;
import com.deimos.HMA;
import com.deimos.UtilsDeimos;
import com.deimos.dataBase.TaskController;

public class GetFeasibility {

    private static HMA hma = new HMA();
    SPSUtils utils = new SPSUtils();
    static String taskId = null;

    @BeforeClass
    public static void init() {
	TaskController taskController = new TaskController();
	taskController.createTables(false);
    }
    
    @Test
    public void GetFeasibility_Asyn_Feasible_Step_1() throws Exception{
	OMElement request, response;
	request = AXIOMUtil.stringToOM(AllTests.readTest(AllTests.test_dir + "GetFeasibility_Asyn_Feasible_Step_1_Request.xml"));
	response = hma.GetFeasibility(request);

	StatusReport report = UtilsDeimos.readParams(response.toString()).getReport();
	
	if (report.getDescription().contains("in use")) {
	    if (report.getDescription().contains("task:"))
		taskId = report.getDescription().split("task:")[1].split(",")[0].replace(" ", "").replace("Z.", "Z");
	    if (report.getDescription().contains("tasks:"))
		taskId = report.getDescription().split("tasks:")[1].split(",")[0].replace(" ", "").replace("Z.", "Z");
	    cancel_is_in_use();
	    taskId = null;
	    GetFeasibility_Asyn_Feasible_Step_1();
	}

	if (taskId == null) {
	    taskId = UtilsDeimos.readParams(response.toString()).getReport().getTaskID();
	    assertTrue(UtilsDeimos.readParams(response.toString()).getReport().getRequestStatus() == RequestStatus.Pending);
	    GetFeasibility_Asyn_Feasible_Step_2();
	    taskId = UtilsDeimos.readParams(response.toString()).getReport().getTaskID();
	}
    }
    
    public void GetFeasibility_Asyn_Feasible_Step_2() throws Exception{
	OMElement request, response;
	request = AXIOMUtil.stringToOM(AllTests.readTest(AllTests.test_dir + "GetFeasibility_Asyn_Feasible_Step_2_Request.xml").replace("TASKID", taskId));
	response = hma.GetStatus(request);
	InputStream is = new ByteArrayInputStream(response.toString().getBytes());
	DOMHelper domResponse = new DOMHelper(is, false);
	RequestStatus status = ((GetStatusResponse) utils.readXMLResponse(domResponse, domResponse.getRootElement(), "SPS", "GetStatusResponse", "2.0")).getReportList().get(0).getRequestStatus();
	assertTrue(status == RequestStatus.Pending);
	
	Thread.sleep(95000);

	request = AXIOMUtil.stringToOM(AllTests.readTest(AllTests.test_dir + "GetFeasibility_Asyn_Feasible_Step_2_Request.xml").replace("TASKID", taskId));
	response = hma.GetStatus(request);
	is = new ByteArrayInputStream(response.toString().getBytes());
	domResponse = new DOMHelper(is, false);
	status = ((GetStatusResponse) utils.readXMLResponse(domResponse, domResponse.getRootElement(), "SPS", "GetStatusResponse", "2.0")).getReportList().get(0).getRequestStatus();
	assertTrue(status == RequestStatus.Accepted);
    }
    
    
    @Test
    public void GetFeasibility_Asyn_Not_Feasible_Step_1() throws Exception{
	OMElement request, response;
	request = AXIOMUtil.stringToOM(AllTests.readTest(AllTests.test_dir + "GetFeasibility_Asyn_Not_Feasible_Step_1_Request.xml"));
	response = hma.GetFeasibility(request);

	StatusReport report = UtilsDeimos.readParams(response.toString()).getReport();

	if (report.getDescription().contains("in use")) {
	    if (report.getDescription().contains("task:"))
		taskId = report.getDescription().split("task:")[1].split(",")[0].replace(" ", "").replace("Z.", "Z");
	    if (report.getDescription().contains("tasks:"))
		taskId = report.getDescription().split("tasks:")[1].split(",")[0].replace(" ", "").replace("Z.", "Z");
	    cancel_is_in_use();
	    taskId = null;
	    GetFeasibility_Asyn_Feasible_Step_1();
	}

	if (taskId == null) {
	    taskId = UtilsDeimos.readParams(response.toString()).getReport().getTaskID();
	    assertTrue(UtilsDeimos.readParams(response.toString()).getReport().getRequestStatus() == RequestStatus.Pending);
	    GetFeasibility_Asyn_Feasible_Step_2();
	}
    }
    
    public void GetFeasibility_Asyn_Not_Feasible_Step_2() throws Exception{
	OMElement request, response;
	request = AXIOMUtil.stringToOM(AllTests.readTest(AllTests.test_dir + "GetFeasibility_Asyn_Not_Feasible_Step_2_Request.xml").replace("TASKID", taskId));
	response = hma.GetStatus(request);
	InputStream is = new ByteArrayInputStream(response.toString().getBytes());
	DOMHelper domResponse = new DOMHelper(is, false);
	RequestStatus status = ((GetStatusResponse) utils.readXMLResponse(domResponse, domResponse.getRootElement(), "SPS", "GetStatusResponse", "2.0")).getReportList().get(0).getRequestStatus();
	assertTrue(status == RequestStatus.Pending);
	
	Thread.sleep(25000);

	request = AXIOMUtil.stringToOM(AllTests.readTest(AllTests.test_dir + "GetFeasibility_Asyn_Feasible_Step_2_Request.xml").replace("TASKID", taskId));
	response = hma.GetStatus(request);
	is = new ByteArrayInputStream(response.toString().getBytes());
	domResponse = new DOMHelper(is, false);
	status = ((GetStatusResponse) utils.readXMLResponse(domResponse, domResponse.getRootElement(), "SPS", "GetStatusResponse", "2.0")).getReportList().get(0).getRequestStatus();
	assertTrue(status == RequestStatus.Rejected);
    }
    
    @Test
    public void GetFeasibility_Syn_Feasible_Request() throws Exception{
	OMElement request, response;
	request = AXIOMUtil.stringToOM(AllTests.readTest(AllTests.test_dir + "GetFeasibility_Syn_Feasible_Request.xml"));
	response = hma.GetFeasibility(request);

	StatusReport report = UtilsDeimos.readParams(response.toString()).getReport();
	
	if (report.getDescription().contains("in use")) {
	    if (report.getDescription().contains("task:"))
		taskId = report.getDescription().split("task:")[1].split(",")[0].replace(" ", "").replace("Z.", "Z");
	    if (report.getDescription().contains("tasks:"))
		taskId = report.getDescription().split("tasks:")[1].split(",")[0].replace(" ", "").replace("Z.", "Z");
	    cancel_is_in_use();
	    taskId = null;
	    GetFeasibility_Syn_Feasible_Request();
	}

	if (taskId == null) {
	    taskId = UtilsDeimos.readParams(response.toString()).getReport().getTaskID();
	    assertTrue(UtilsDeimos.readParams(response.toString()).getReport().getRequestStatus() == RequestStatus.Accepted);
	}
    }
    
    @Test
    public void GetFeasibility_Syn_Not_Feasible() throws Exception{
	OMElement request, response;
	request = AXIOMUtil.stringToOM(AllTests.readTest(AllTests.test_dir + "GetFeasibility_Syn_Not_Feasible_Request.xml"));
	response = hma.GetFeasibility(request);

	StatusReport report = UtilsDeimos.readParams(response.toString()).getReport();
	
	if (report.getDescription().contains("in use")) {
	    if (report.getDescription().contains("task:"))
		taskId = report.getDescription().split("task:")[1].split(",")[0].replace(" ", "").replace("Z.", "Z");
	    if (report.getDescription().contains("tasks:"))
		taskId = report.getDescription().split("tasks:")[1].split(",")[0].replace(" ", "").replace("Z.", "Z");
	    cancel_is_in_use();
	    taskId = null;
	    GetFeasibility_Syn_Not_Feasible();
	}

	if (taskId == null) {
	    taskId = UtilsDeimos.readParams(response.toString()).getReport().getTaskID();
	    assertTrue(UtilsDeimos.readParams(response.toString()).getReport().getRequestStatus() == RequestStatus.Rejected);
	}
    }
    
    @After
    public void cancel_is_in_use() throws Exception {
	OMElement request;
	request = AXIOMUtil.stringToOM(AllTests.readTest(AllTests.test_dir + "Confirm_Step_3_Cancel_Request.xml").replace("TASKID", taskId));
	hma.Cancel(request);
    }
    
}
